/**
 * Copyright (c) 2018-2024, NXOS Development Team
 * SPDX-License-Identifier: Apache-2.0
 * 
 * Contains: json from xboot
 * 
 * Change Logs:
 * Date           Author            Notes
 * 2024-1-4       JasonHu           Init
 */

#ifndef __JSON_H__
#define __JSON_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <nx_configure.h>

#ifdef CONFIG_NX_DEVICE_TREE_SUPPORT

#include <base/types.h>

struct JSON_Value;
struct JSON_ObjectEntry;

enum JSON_Type {
	JSON_NONE		= 0,
	JSON_OBJECT		= 1,
	JSON_ARRAY		= 2,
	JSON_INTEGER	= 3,
	JSON_DOUBLE		= 4,
	JSON_STRING		= 5,
	JSON_BOOLEAN	= 6,
	JSON_NULL		= 7,
};

struct JSON_Value {
	struct JSON_Value * parent;
	enum JSON_Type type;

	union {
		int boolean;
		NX_I64 integer;
		double dbl;

		struct {
			NX_U32 length;
			char * ptr;
		} string;

		struct {
			NX_U32 length;
			struct JSON_ObjectEntry * values;
		} object;

		struct {
			NX_U32 length;
			struct JSON_Value ** values;
		} array;
	} u;

	union {
		struct JSON_Value * next_alloc;
		void * object_mem;
	} reserved;
};

struct JSON_ObjectEntry {
	char * name;
	NX_U32 name_length;
	struct JSON_Value * value;
};

struct JSON_Value * JSON_Parse(const char * json, NX_Size length, char * errbuf);
void JSON_Free(struct JSON_Value * value);

#endif

#ifdef __cplusplus
}
#endif

#endif /* __JSON_H__ */
